<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8" />
    <title>Item Sliding - Async</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
    />
    <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
    <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
    <script src="../../../../../scripts/testing/scripts.js"></script>
    <script nomodule src="../../../../../dist/ionic/ionic.js"></script>
    <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>

    <style>
      .hidden {
        display: none;
      }
    </style>
  </head>

  <body>
    <ion-app>
      <ion-header>
        <ion-toolbar>
          <ion-title>Item Sliding - Basic</ion-title>
        </ion-toolbar>
      </ion-header>

      <ion-content>
        <ion-button id="toggle-button" onclick="toggle()">Make async changes</ion-button>
        <ion-list>
          <ion-item-sliding id="async-item">
            <ion-item-options side="end">
              <ion-item-option>Option</ion-item-option>
            </ion-item-options>
          </ion-item-sliding>
          <ion-item-sliding id="async-options-added">
            <ion-item>ion-item-options added async</ion-item>
          </ion-item-sliding>
          <ion-item-sliding id="async-options-removed">
            <ion-item>ion-item-options removed async</ion-item>
            <ion-item-options side="end">
              <ion-item-option>Option</ion-item-option>
            </ion-item-options>
          </ion-item-sliding>
        </ion-list>

        <script>
          const slidingAsyncItem = document.querySelector('#async-item');
          const slidingAsyncOptionsAdded = document.querySelector('#async-options-added');
          const slidingAsyncOptionsRemoved = document.querySelector('#async-options-removed');

          [slidingAsyncOptionsAdded, slidingAsyncOptionsRemoved].forEach((itemSliding) => {
            itemSliding.querySelector('ion-item').onclick = () => {
              itemSliding.open('end');
            };
          });

          /**
           * Function used rather than setTimeout to ensure ion-item-slidings
           * are fully inited before making changes. Otherwise some bugs would
           * not appear or be flaky.
           */
          function toggle() {
            const asyncItem = document.createElement('ion-item');
            asyncItem.innerText = 'ion-item added async';
            slidingAsyncItem.appendChild(asyncItem);

            asyncItem.onclick = () => {
              slidingAsyncItem.open('end');
            };

            const asyncOptions = document.createElement('ion-item-options');
            asyncOptions.setAttribute('side', 'end');
            asyncOptions.innerHTML = `<ion-item-option>Option</ion-item-option>`;
            slidingAsyncOptionsAdded.appendChild(asyncOptions);

            slidingAsyncOptionsRemoved.querySelector('ion-item-options').remove();

            const toggleButton = document.querySelector('#toggle-button');
            toggleButton.classList.add('hidden');
          }
        </script>
      </ion-content>
    </ion-app>
  </body>
</html>
